package com.wwy.controller.sys;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.wwy.api.CacheService;
import com.wwy.dto.sys.UserDto;
import com.wwy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class TestController {

    @Value("${redis.host}")
    private String host;

    @Autowired
    private UserService userService;

    @Autowired
    private CacheService cacheService;

    /**
     * 获取用户信息
     * @param userId
     * @return
     */
    @RequestMapping("/getUser")
    public String getUser(Integer userId){
        UserDto user =  userService.getUser(userId);
        return "成功返回用户:" + user.getUserName();
    }

    /**
     * 设置缓存数据
     * @return
     */
    @RequestMapping("/setCache")
    public String setCache(String key){
        //UserDto user =  userService.getUser(1);
        //cacheService.put("user2",JSON.toJSONString(user));
        cacheService.put(key,key+"_success");
        return "success";
    }

    /**
     * 获取缓存数据
     * @param key
     * @return
     */
    @RequestMapping("/getCache")
    public String getCache(String key){
        UserDto cache = cacheService.getCache(key, UserDto.class);
        System.out.println("缓存数据：" + cache);
        return cache.toString();
    }


}
